package tech.dongliang.common.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.IContext;
import tech.dongliang.common.config.EmailConfig;
import tech.dongliang.common.enums.EBizError;
import tech.dongliang.common.exception.BizException;
import tech.dongliang.common.service.IEmailService;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;

@Service
public class EmailServiceImpl implements IEmailService {
    @Autowired
    private EmailConfig emailConfig;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void sendSimpleMail(String sendTo, String titel, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailConfig.getEmailFrom());
        message.setTo(sendTo);
        message.setSubject(titel);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendAttachmentsMail(String sendTo, String titel, String content, Map<String, File> attachments) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(emailConfig.getEmailFrom());
            helper.setTo(sendTo);
            helper.setSubject(titel);
            helper.setText(content);
            if (attachments != null) {
                for (Map.Entry<String, File> pair : attachments.entrySet()) {
                    helper.addAttachment(pair.getKey(), new FileSystemResource(pair.getValue()));
                }
            }
        } catch (Exception e) {
            throw new BizException(EBizError.UNKNOWE_ERROR);
        }
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendTemplateMail(String sendTo, String title, String template, IContext content, Map<String, File> attachments) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(emailConfig.getEmailFrom());
            helper.setTo(sendTo);
            helper.setSubject(title);
            String text = templateEngine.process(template, content);
            helper.setText(text, true);
            if (attachments != null) {
                for (Map.Entry<String, File> pair : attachments.entrySet()) {
                    helper.addAttachment(pair.getKey(), new FileSystemResource(pair.getValue()));
                }
            }
        } catch (Exception e) {
            throw new BizException(EBizError.UNKNOWE_ERROR);
        }
        mailSender.send(mimeMessage);
    }
}
